package com.gec.hawaste.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.hawaste.entity.SysRole;
import com.gec.hawaste.entity.SysRoleDo;
import com.gec.hawaste.entity.WorkOrderDo;
import com.gec.hawaste.service.ISysRoleService;
import com.gec.hawaste.service.ISysUserRoleService;
import com.gec.hawaste.utils.PageInfo;
import com.gec.hawaste.utils.ResultBean;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author gec
 * @since 2022-11-23
 */
@RestController
@RequestMapping("/manager/role")
public class SysRoleController {
    @Autowired
    ISysRoleService sysRoleService;
    @Autowired
    ISysUserRoleService sysUserRoleService;

    @RequiresPermissions("role:select")
    @GetMapping("/select/{current}/{size}")
    public ResultBean<Page> select(@PathVariable Integer current,
                                   @PathVariable Integer size,
                                   @RequestParam Map<String, Object> params) {
        PageInfo<SysRoleDo> page = (PageInfo<SysRoleDo>) sysRoleService.selectByCondition(new PageInfo<SysRoleDo>(current, size), params);
        page.setNavigatePage();
        return ResultBean.ok(page);
    }

    @RequiresPermissions("role:select")
    @RequestMapping("/deleteBatch")
    public ResultBean deleteBatch(Long rid, Long[] ids) {
        return ResultBean.ok(sysUserRoleService.deleteBatch(rid, ids));
    }

    @RequiresPermissions("role:select")
    @RequestMapping("/insertBatch")
    public ResultBean insertBatch(Long rid, Long[] ids) {
        return ResultBean.ok(sysUserRoleService.insertBatch(rid, ids));
    }

    @RequiresPermissions("role:select")
    @PostMapping("/saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody SysRoleDo sysRole, HttpSession session) {
        //更新创建人   从状态对象中获取user名   后续完成登录功能后实现
        sysRole.setCreateBy("gec");
//        SysUser loginUser = (SysUser) session.getAttribute("loginUser");
//        if (sysRole.getId()==null){
//            sysRole.setCreateBy(loginUser.getName());
//        }
        //无异常直接返回，出异常由统一异常处理Controller处理返回
        //公共serviceImpl生成的添加或更新方法  有主键是更新，无主键值是添加
        sysRoleService.updateById(sysRole);
        return ResultBean.ok();
    }

    @RequiresPermissions("role:select")
    @RequestMapping("/selectOne")
    public ResultBean selectOne(Long id) {
        return ResultBean.ok(sysRoleService.selectOne(id));
    }


    @RequiresPermissions("role:select")
    @RequestMapping("/delete/{rid}")
    public ResultBean delete(@PathVariable Long rid) {
        System.out.println("ssss");
        return ResultBean.ok(sysRoleService.delete(rid));
    }
}
